# author: dcc

import random
from utils.common_dcc import response_time
from utils.environments_json import urlHead


# /topicapi/chapterPractice/front/submit（以章节练习提交的接口提交作答，生成错题）
# otopic_ids和answer的长度必须一致
def chapter_submit(uri: str, body: dict, otopic_ids: list, answer: list, section_id: int, session):
    url = urlHead+uri
    otopic_list = []  # 提交的客观题作答详情
    for i in range(len(otopic_ids)):
        dict_otopic = {
                "topicId": otopic_ids[i],
                "topicNum": i+1,
                "isFlag": random.randint(0, 1),
                "isAnswer": 1,
                "answer": answer[i],
                "costTime": 1
                }
        otopic_list.append(dict_otopic)
    body['chapterId'] = section_id
    body['otopicList'] = otopic_list
    body['totalTime'] = len(otopic_ids)
    res = session.post(url=url, json=body)
    response_time(url=url, res=res)
    return res


# /topicapi/userOtopicRecord/front/v2/listV2RecentlyWrongTopicSummary（错题本“最新”列表下数据请求）
def wrong_new(uri: str, body: dict, session):
    url = urlHead+uri
    res = session.post(url=url, json=body)
    response_time(url=url, res=res)
    return res


# /topicapi/userOtopicRecord/front/selectUserOtopicTree （“章节”列表下数据请求,type_all错题1，收藏2，笔记3）
def chapter_tree(uri: str, body: dict, user_id: int, type_all: int, session):
    url = urlHead+uri
    body["userId"] = user_id
    body["type"] = type_all
    res = session.post(url=url, json=body)
    response_time(url=url, res=res)
    return res


# /userapi/userFront/updateGlobalSetting (更新全局设置，改变错题本错误次数筛选)
def update_setting(uri: str, body: dict, wrong_times: int, remove_wrong_model: int, session):
    url = urlHead+uri
    body["wrongTimes"] = wrong_times
    body["removeWrongModel"] = remove_wrong_model
    res = session.post(url=url, json=body)
    response_time(url=url, res=res)
    return res


# /topicapi/userNote/front/fetchNoteOtopicInfoByChapterAnswerModel （根据节，出客观题）
def fetch_topic(uri: str, body: dict, chapter_id: int, type_all: int, session):
    url = urlHead+uri
    body["chapterId"] = chapter_id
    body["type"] = type_all
    res = session.post(url=url, json=body)
    response_time(url=url, res=res)
    return res


# /topicapi/userOtopicRecord/front/submit （错题，最新下交卷）
def submit_new(uri: str, body: dict, topic_id: int, remove_wrong_model: int, title: str, session):
    url = urlHead+uri
    body["title"] = title
    body["removeWrongModel"] = remove_wrong_model
    body["otopicList"][0]["topicId"] = topic_id
    res = session.post(url=url, json=body)
    response_time(url=url, res=res)
    return res


# /topicapi/userOtopicRecord/front/submit （错题，章节下交卷）
def submit_wrong(uri: str, body: dict, topic_id: int, remove_wrong_model: int, chapter_id: int, session):
    url = urlHead+uri
    body["chapterId"] = chapter_id
    body["removeWrongModel"] = remove_wrong_model
    body["otopicList"][0]["topicId"] = topic_id
    res = session.post(url=url, json=body)
    response_time(url=url, res=res)
    return res


# /topicapi/tmsExam/front/frontListExamRecord （做题记录查询）
def wrong_record(uri: str, body: dict, type_record: int, session):
    url = urlHead+uri
    body["type"] = type_record
    res = session.post(url=url, json=body)
    response_time(url=url, res=res)
    return res


# /fileapi/wrongBook/front/batchExportPdf （错题本导出）
def export(uri: str, body: dict, section_id: int, session):
    url = urlHead+uri
    body["sectionIds"] = section_id
    res = session.get(url=url, params=body)
    response_time(url=url, res=res)
    return res


# /topicapi/userOtopicRecord/front/clearWrongOtpoics （清空错题本）
def clear(uri: str, body: dict, session):
    url = urlHead+uri
    res = session.get(url=url, params=body)
    response_time(url=url, res=res)
    return res


# /topicapi/tmsUserCollection/front/insertUserCollection (题目收藏，type：1客观题，2主观题)
def collect(uri: str, body: dict, topic_id: int, user_id: int, type_topic: int, session):
    url = urlHead+uri
    body["topicId"] = topic_id
    body["userId"] = user_id
    body["type"] = type_topic
    res = session.post(url=url, json=body)
    response_time(url=url, res=res)
    return res


# /topicapi/tmsUserCollection/front/recentCollectionTopics (收藏“最新”数据请求)
def collect_new(uri: str, body: dict, user_id: int, session):
    url = urlHead+uri
    body["userId"] = user_id
    res = session.post(url=url, json=body)
    response_time(url=url, res=res)
    return res


# /topicapi/tmsUserCollection/front/submit （收藏“最新”列表下，作答提交）
def submit_collect_new(uri: str, body: dict, chapter_id: int, remove_wrong_model: int, topic_id: int, session):
    url = urlHead+uri
    body["chapterId"] = chapter_id
    body["removeWrongModel"] = remove_wrong_model
    body["otopicList"][0]["topicId"] = topic_id
    res = session.post(url=url, json=body)
    response_time(url=url, res=res)
    return res


# /fileapi/collectionTopic/front/batchExportPdf (收藏、笔记的客观题导出，type_all，1收藏，2笔记)
def export_collect_and_note(uri: str, body: dict, section_id: int, type_all: int, session):
    url = urlHead+uri
    body["sectionIds"] = section_id
    body["type"] = type_all
    res = session.get(url=url, params=body)
    response_time(url=url, res=res)
    return res


# /fileapi/collectionTopic/front/batchStopicExportPdf (收藏、笔记的主观题导出，type_all，1收藏，2笔记)
def export_stopic(uri: str, body: dict, stopic_id: int, type_all: int, session):
    url = urlHead+uri
    body["stopicIds"] = stopic_id
    body["type"] = type_all
    res = session.get(url=url, params=body)
    response_time(url=url, res=res)
    return res


# /topicapi/tmsUserCollection/front/selectStopicGroupList （收藏下，主观题栏，stype：1主观题，2速刷题）
def stopic_chapter_list(uri: str, body: dict, user_id: int, stype: int, session):
    url = urlHead + uri
    body["userId"] = user_id
    body["stype"] = stype
    res = session.post(url=url, json=body)
    response_time(url=url, res=res)
    return res


# /topicapi/userNote/front/findCollectionOrNoteByChapterIdAndUserId （根据科目id 列出主观题信息。type_all：2收藏，3笔记。
# stype：1主观题，2速刷题）
def stopic_info_list(uri: str, body: dict, chapter_id: int, type_all: int, stype: int, session):
    url = urlHead + uri
    body["chapterId"] = chapter_id
    body["type"] = type_all
    body["stype"] = stype
    res = session.post(url=url, json=body)
    response_time(url=url, res=res)
    return res


# /topicapi/tmsUserCollection/front/cancleUserCollection （取消收藏，type_all，1客观题，2主观题）
def un_collect(uri: str, body: dict, topic_id: int, user_id: int, type_all: int, session):
    url = urlHead+uri
    body["topicId"] = topic_id
    body["userId"] = user_id
    body["type"] = type_all
    res = session.post(url=url, json=body)
    response_time(url=url, res=res)
    return res


# /topicapi/userNote/front/insertUserNote （笔记添加,type_all，1客观题，2主观题）
def add_note(uri: str, body: dict, topic_id: int, user_id: int, type_all: int, session, **kwargs):
    url = urlHead+uri
    body["topicId"] = topic_id
    body["userId"] = user_id
    body["type"] = type_all
    body.update(kwargs)
    res = session.post(url=url, json=body)
    response_time(url=url, res=res)
    return res


# /topicapi/userNote/front/recentNoteTopics (笔记“最新”列表数据请求)
def note_new(uri: str, body: dict, user_id: int, session):
    url = urlHead+uri
    body["userId"] = user_id
    res = session.post(url=url, json=body)
    response_time(url=url, res=res)
    return res


# /topicapi/userOtopicRecord/front/clearOneWrongOtpoic (手动调接口移除错题)
def move_wrong(uri: str, body: dict, topic_id: int, session):
    url = urlHead+uri
    body["topicId"] = topic_id
    res = session.get(url=url, params=body)
    response_time(url=url, res=res)
    return res


# /topicapi/userNote/front/selectStopicGroupList （笔记主观题列表接口）
def note_stopic_list(uri: str, body: dict, user_id: int, stype: int, session):
    url = urlHead+uri
    body["userId"] = user_id
    body["stype"] = stype
    res = session.post(url=url, json=body)
    response_time(url=url, res=res)
    return res


# /topicapi/userNote/front/fetchNoteStopicInfoByChapter (笔记，根据科目id查询主观题解析)
def note_stopic_info(uri: str, body: dict, chapter_id: int, type_all: int, stype: int, session):
    url = urlHead+uri
    body["chapterId"] = chapter_id
    body["type"] = type_all
    body["stype"] = stype
    res = session.post(url=url, json=body)
    response_time(url=url, res=res)
    return res
